package com.gray.wow.biz.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gray.wow.biz.ContentBiz;
import com.gray.wow.common.DateUtil;
import com.gray.wow.common.Status;
import com.gray.wow.dao.impl.ContentDaoImpl;
import com.gray.wow.domain.Content;
import com.gray.wow.domain.Domain;
import com.gray.wow.domain.PageModel;
import com.gray.wow.domain.Tag;

@Service("contentBiz")
@Transactional
public class ContentBizImpl implements ContentBiz {

	@Resource
	private ContentDaoImpl contentDao;

	@Override
	public PageModel getContent(Integer pageSize, Integer page,
			String openTime, String status, String maxTime, Integer tag) {
		Map<String, Object> map = new HashMap<String, Object>();
		int count = 0;
		if (pageSize != null) {
			page = page == null ? 1 : page;
			count = contentDao.findCountByCondition(map);
			map.put("startId", (page - 1) * pageSize);
			map.put("pageSize", pageSize);
		}
		if (openTime != null && !openTime.equals("")) {
			map.put("openTime", openTime);
		}
		if (maxTime != null && !maxTime.equals("")) {
			map.put("maxTime", maxTime);
		}
		if (status != null && !status.equals("")) {
			map.put("status", status);
		}
		if (tag != null) {
			map.put("tag", tag);
		}
		map.put("startId", (page - 1) * pageSize);
		map.put("pageSize", pageSize);
		List<Content> list = contentDao.findListByCondition(map);
		PageModel pm = PageModel.getPageModel(pageSize, page, count, list);
		return pm;
	}

	@Override
	public boolean saveContent(Content content) {
		if (content != null) {
			if (content.getId() != null && !content.getId().equals("")) {
				if (contentDao.update(content) == 1) {
					return true;
				}
			} else {
				if (contentDao.add(content) == 1) {
					return true;
				}
			}
		}
		return false;
	}

	@Override
	public boolean delContent(int... ids) {
		if (ids != null && ids.length > 0) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("ids", ids);
			if (contentDao.delete(map) == ids.length) {
				return true;
			}
		}
		return false;
	}

	@Override
	public boolean approveContent(int... ids) {
		if (ids != null && ids.length > 0) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("ids", ids);
			map.put("status", Status.PASS.getValue());
			if (contentDao.update(map) == ids.length)
				return true;
		}
		return false;
	}

	@Override
	public boolean tagContent(String tag, int... ids) {
		if (ids != null && ids.length > 0) {
			if (tag != null && !tag.equals("")) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("ids", ids);
				map.put("tag", tag);
				if (contentDao.update(map) == ids.length)
					return true;
			}
		}
		return false;
	}

	@Override
	public boolean deadlineContent(String deadline, int... ids) {
		if (ids != null && ids.length > 0) {
			if (deadline != null && !deadline.equals("")) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("ids", ids);
				map.put("deadline", deadline);
				if (contentDao.update(map) == ids.length)
					return true;
			}
		}
		return false;
	}

	@Override
	public Content getContentById(Integer id) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		List<Content> list = contentDao.findListByCondition(map);
		if (list != null && list.size() == 1) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public PageModel getContentByTag(String tag, Integer pageSize) {
		Map<String, Object> map = new HashMap<String, Object>();
		// 精品置顶-精品推荐-史诗传说-蓝色推荐
		map.put("tag", tag);// 类别
		map.put("status", Status.PASS.getValue());// 已经审核过
		map.put("deadline", DateUtil.getDateStr("yyyy-MM-dd", new Date()));// 当日的时间在该数据的广告时间内
		map.put("pageSize", pageSize);
		List<Content> list = contentDao.getContentByTag(map);
		PageModel pm = new PageModel();
		pm.setList(list);
		pm.setPageSize(pageSize);
		return pm;
	}

	@Override
	public PageModel getContentByOpenTime(String openTime, String maxTime,
			Integer pageSize) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("status", Status.PASS.getValue());// 已经审核过
		map.put("pageSize", pageSize);
		map.put("openTime", openTime);
		map.put("maxTime", maxTime);
		List<Content> list = contentDao.findListByCondition(map);
		PageModel pm = new PageModel();
		pm.setList(list);
		pm.setPageSize(pageSize);
		return pm;
	}

	@Override
	public Domain getMyDomain() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("domainType", Domain.LINK_MY);
		List<Domain> list = contentDao.getDomain(map);
		if (list != null && list.size() == 1) {
			return list.get(0);
		}
		return null;

	}

	@Override
	public boolean saveDomain(Domain domain) {

		if (domain != null) {
			if (domain.getId() != null && !domain.getId().equals("")) {
				if (contentDao.updateDomain(domain) == 1)
					return true;
			} else {
				if (contentDao.insertDomain(domain) == 1)
					return true;
			}
		}
		return false;
	}

	@Override
	public Domain getDomainById(Integer id) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		List<Domain> list = contentDao.getDomain(map);
		if (list != null && list.size() == 1) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public PageModel getDomain(Integer page, Integer pageSize, String domainType) {
		Map<String, Object> map = new HashMap<String, Object>();
		int count = 0;
		if (pageSize != null) {
			page = page == null ? 1 : page;
			count = contentDao.getDomainCount(map);
			map.put("startId", (page - 1) * pageSize);
			map.put("pageSize", pageSize);
		}
		map.put("domainType", domainType);
		List<Domain> list = contentDao.getDomain(map);
		PageModel pm = PageModel.getPageModel(pageSize, page, count, list);
		return pm;
	}

	@Override
	public List<Tag> getTagList() {
		List<Tag> list = contentDao.getTag(null);
		return list;
	}

	@Override
	public boolean updateTag(Tag tag) {
		if (contentDao.updateTag(tag) == 1)
			return true;
		return false;

	}

	@Override
	public List<Tag> getTagWidthContent() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("status", Status.PASS.getValue());// 已经审核过
		map.put("deadline", DateUtil.getDateStr("yyyy-MM-dd", new Date()));// 当日的时间在该数据的广告时间内
		List<Tag> list = contentDao.getTagWidthContent(map);
		return list;
	}

	@Override
	public boolean deleteDomain(int... ids) {
		if (ids != null && ids.length > 0) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("ids", ids);
			if (contentDao.deleteDomain(map) == ids.length)
				return true;
		}
		return false;
	}

	@Override
	public PageModel getFooterLink(Integer page, Integer pageSize) {

		return getDomain(page, pageSize, Domain.LINK_FOOTER);
	}

	@Override
	public List<Domain> getFooterLink() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("domainType", Domain.LINK_FOOTER);
		List<Domain> list = contentDao.getDomain(map);
		return list;
	}

}
